﻿using DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BussinessLogicLayer
{
    public class PhanThuongBLL
    {
        static QuanLyGiaiDauDataContext QLGD = new QuanLyGiaiDauDataContext();
        public static List<PhanThuong> LayPhanThuong(int MaGD)
        {
            var phanthuong = from pt in QLGD.PhanThuongs
                          where pt.TrangThai != "Đã xóa" && pt.MaGiaiDau == MaGD
                          select pt;
            return phanthuong.ToList();
        }
        public static bool ThemPhanThuong(PhanThuong entity)
        {
            try
            {
                PhanThuong PT = new PhanThuong();
                PT.MaPhanThuong = entity.MaPhanThuong;
                PT.TenPhanThuong = entity.TenPhanThuong;
                PT.TraoThuong = entity.TraoThuong;
                PT.MoTa = entity.MoTa;
                PT.TrangThai = entity.TrangThai;
                PT.MaGiaiDau = entity.MaGiaiDau;
                QLGD.PhanThuongs.InsertOnSubmit(PT);
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool CapNhatPT(PhanThuong entity)
        {
            try
            {
                PhanThuong CapNhatPT = (from pt in QLGD.PhanThuongs
                                     select pt).Single(pthuong => pthuong.MaPhanThuong == entity.MaPhanThuong);
                CapNhatPT.TenPhanThuong = entity.TenPhanThuong;
                CapNhatPT.TraoThuong = entity.TraoThuong;
                CapNhatPT.MoTa = entity.MoTa;
                CapNhatPT.TrangThai = entity.TrangThai;
                CapNhatPT.MaGiaiDau = entity.MaGiaiDau;
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
        public static bool XoaPhanThuong(int MaPT)
        {
            try
            {
                PhanThuong XoaPT = (from pt in QLGD.PhanThuongs
                                    select pt).Single(pthuong => pthuong.MaPhanThuong == MaPT);
                XoaPT.TrangThai = "Đã xóa";
                QLGD.SubmitChanges();
                int kq = 1;
                return kq > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
    }
}
